package com.qianfeng.smsplatform.monitor.config;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.qianfeng.smsplatform.monitor.job.MonitorQueueSizeJob;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by jackiechan on 2021/4/12 21:10
 *
 * @author jackiechan
 */
@Configuration
public class ElasticJobConfig {

    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter zookeeperRegistryCenter(@Value("${regCenter.serverList}") String serverList, @Value("${regCenter.namespace}") String namespace) {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverList, namespace);
        ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        return zookeeperRegistryCenter;
    }

    @Bean(initMethod = "init")
    public JobScheduler jobScheduler(MonitorQueueSizeJob job, ZookeeperRegistryCenter zookeeperRegistryCenter,LiteJobConfiguration liteJobConfiguration) {

        return  new SpringJobScheduler(job, zookeeperRegistryCenter, liteJobConfiguration);
    }

    @Bean
    public LiteJobConfiguration liteJobConfiguration(JobTypeConfiguration jobTypeConfiguration) {
        return LiteJobConfiguration.newBuilder(jobTypeConfiguration).build();
    }

    @Bean
    public JobTypeConfiguration jobTypeConfiguration(JobCoreConfiguration jobCoreConfiguration) {
        return new SimpleJobConfiguration(jobCoreConfiguration, MonitorQueueSizeJob.class.getCanonicalName());
    }

    @Bean
    public JobCoreConfiguration jobCoreConfiguration(@Value("${job.cron}") String corn, @Value("${job.shardingcout}")  int shardingCount, @Value("${job.shardingparams}")String shardingparams) {

        return JobCoreConfiguration.newBuilder("MonitorQueueSizeJob", corn, shardingCount).shardingItemParameters(shardingparams).build();
    }
}
